// pages/EchartsPrints/DDLine/DDLine.js
import * as echarts from '../../../ec-canvas/echarts.js';
const app = getApp();
let chart = null;
let pageInstance = {}
var option = {
  tooltip : {
    
      trigger: 'axis'
  },
  legend: {
      data:['死亡人数','感染人数']
  },
 
  calculable : true,
  xAxis : [
      {
          type : 'category',
          data : [],
          axisLabel: {
            show: true,
             textStyle: {
               fontSize : 8     //更改坐标轴文字大小
             }
          }
      
    }
  ],
  yAxis : [
      {
          type : 'value'
      }
  ],
  series : [
      {
          name:'死亡人数',
          type:'bar',
          data:[]
      },
      {
          name:'感染人数',
          type:'bar',
          data:[]
      }
  ]
};
function initChart(canvas, width, height) {
   chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);

  

  chart.setOption(option);
  return chart;
}
function MyDeadSort(a,b){
  return a.conNum > b.conNum ? 1:-1
}
Component({
  /**
   * 组件的属性列表
   */
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  properties: {
    region:{
      type:String,
      value:'省',
      observer: function(newVal, oldVal) {
        // 属性值变化时执行,更新折线图 写在此部分
        console.log('省份更新为' + newVal)
      }
    },
    citycounts:{
      type:Array,
      observer: function(newVal, oldVal) {
        var len = newVal.length > 5 ? 5:newVal.length;
        newVal.sort(MyDeadSort);
        var xAxis = [];
        var xGet = [];
        var xDead = [];
        for(var i = 0; i < len ; i++){
          xAxis.push(newVal[i].name)
          xGet.push(newVal[i].conNum)
          xDead.push(newVal[i].deathNum)
        }
        option.xAxis[0].data = xAxis;
        option.series[0].data = xDead;
        option.series[1].data = xGet;
        if(len!=0){
          chart.setOption(option)
        }
    }
  }
},

  /**
   * 组件的初始数据
   */
  data: {
    ec: {
      onInit: initChart
    },
  },
  lifetimes: {
    created: function () {
       pageInstance = this;
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {

    echartInit (e) {
      initChart(e.detail.canvas, e.detail.width, e.detail.height);
    }
  }
})
